package com.bosssoft.trainee.nontax.controller;


import com.alibaba.fastjson.JSON;
import com.bosssoft.trainee.nontax.entity.dto.DepartmentDTO;
import com.bosssoft.trainee.nontax.service.DepartmentService;
import com.bosssoft.trainee.nontax.utils.Result;
import com.bosssoft.trainee.nontax.entity.vo.query.DepartmentQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sct
 * @since 2022-06-15
 */
@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**
     * 查询部门列表
     */
    @GetMapping("/list")
    public Result<Object> list(DepartmentQueryVO departmentQueryvo){
        DepartmentDTO departmentDTO = new DepartmentDTO();
        BeanUtils.copyProperties(departmentQueryvo,departmentDTO);
        List<DepartmentDTO> departmentDTOList =departmentService.findDepartmentList(departmentDTO);
        List<DepartmentQueryVO> departmentQueryVOList;
        departmentQueryVOList = JSON.parseArray(JSON.toJSONString(departmentDTOList),DepartmentQueryVO.class);
        return Result.ok(departmentQueryVOList);
    }

    /**
       * 查询上级部门列表
       */
    @GetMapping("/parent/list")
    public Result<Object> getParentDepartment(){
        //调用查询上级部门列表方法
        List<DepartmentDTO> departmentDTOList = departmentService.findParentDepartment();
        List<DepartmentQueryVO> departmentQueryVOList;
        departmentQueryVOList = JSON.parseArray(JSON.toJSONString(departmentDTOList),DepartmentQueryVO.class);
        //返回数据
        return Result.ok(departmentQueryVOList);
    }

    /**
     * 添加部门
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody DepartmentQueryVO departmentQueryVO){
        DepartmentDTO departmentDTO = new DepartmentDTO();
        BeanUtils.copyProperties(departmentQueryVO,departmentDTO);
        if(departmentService.save(departmentDTO)){
            return Result.ok().message("部门添加成功");
        }
        return Result.error().message("部门添加失败");
    }

    /**
     * 修改部门
     */
    @PutMapping("/update")
    public Result<Object> update(@RequestBody DepartmentQueryVO departmentQueryVO){
        DepartmentDTO departmentDTO = new DepartmentDTO();
        BeanUtils.copyProperties(departmentQueryVO,departmentDTO);
        if(departmentService.updateById(departmentDTO)){
            return Result.ok().message("部门修改成功");
        }
        return Result.error().message("部门修改失败");
    }

    /**
     * 查询某个部门下是否存在子部门
     * @param id 部门id
     * @return json部门是否存在信息
     */
    @GetMapping("/check/{id}")
    public Result<Object> check(@PathVariable Long id){
        if(departmentService.hasChildrenOfDepartment(id)){
            return Result.exist().message("该部门下存在子部门，无法删除");
        }
        if(departmentService.hasUserOfDepartment(id)){
            return Result.exist().message("该部门下存在用户，无法删除");
        }
        return Result.ok();
    }


    /**
     * 删除部门
     * @param id 部门id
     */
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Long id){
        if(departmentService.removeById(id)){
            return Result.ok().message("部门删除成功");
        }
        return Result.error().message("部门删除失败");
    }
}

